package com.example.nationmobile;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public class FavoritesActivity extends Activity implements OnClickListener,OnItemClickListener{

	
	private TextView txt_daytime;
	private FrameLayout Img_home,Img_opinion,Img_mul,Img_favorite,Img_setting; 
	public  ArrayList<String> NameFileFavoriteInDir=new ArrayList<String>();
	
	
	//==================ListView======================
	private ListView lstView;
	private OpiListAdapter adapter;
	private ArrayList<HashMap<String, Object>> MyArrList_home;
	private HashMap<String, Object> map_1 = new HashMap<String, Object>();
	private	ProgressDialog mProgressDialog;

	
	//===========================List====================
	public  ArrayList<String> List_title=new ArrayList<String>();
	public  ArrayList<String> List_description=new ArrayList<String>();
	public  ArrayList<String> List_detail=new ArrayList<String>();
	public  ArrayList<String> List_photo=new ArrayList<String>();
	public  ArrayList<String> List_link=new ArrayList<String>();
	public  ArrayList<String> List_thumbnail=new ArrayList<String>();
	
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (android.os.Build.VERSION.SDK_INT > 9) {
			StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
					.permitAll().build();
			StrictMode.setThreadPolicy(policy);
		}
		
		Log.e("evt", "====================onCreate====================");
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_favorites);
		
		
		GetCountFileInDir();
		
		
		//==============Tab=====================
		Img_home=(FrameLayout)findViewById(R.id.Img_home);
		Img_opinion=(FrameLayout)findViewById(R.id.Img_opinion);
		Img_mul=(FrameLayout)findViewById(R.id.Img_mul);
		Img_favorite=(FrameLayout)findViewById(R.id.Img_favorite);
		Img_setting =(FrameLayout)findViewById(R.id.Img_setting);
				
		Img_home.setOnClickListener(this);
		Img_opinion.setOnClickListener(this);
		Img_mul.setOnClickListener(this);
		//Img_favorite.setOnClickListener(this);
		Img_setting .setOnClickListener(this);
				
				
		
		
		
		if (lstView == null) {
			lstView = (ListView) findViewById(R.id.list_view);
			setProgressBarIndeterminateVisibility(true);
			lstView.setOnItemClickListener(this);
			 //=====================ProgressDialog==========================
	        mProgressDialog = new ProgressDialog(this);
	        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);         
	        mProgressDialog.setMessage("Loading......");                       
	        mProgressDialog.setCancelable(false);  
	        
	        
	      
	        
			
		}
		if (MyArrList_home == null)
			MyArrList_home = new ArrayList<HashMap<String, Object>>();
		
		
		
		new LoadFavorite().execute();
		
	}
	
	
	private String PathFile="/mnt/sdcard/Android/data/com.example.nationmobile/cache/Favotite/";
	
	private void GetCountFileInDir()
	{
		

		File wallpaperDirectory = new File("/mnt/sdcard/Android/data/com.example.nationmobile/cache/Favotite");
		// have the object build the directory structure, if needed.
		wallpaperDirectory.mkdirs();
		
		NameFileFavoriteInDir.clear();
		File folder = new File("/mnt/sdcard/Android/data/com.example.nationmobile/cache/Favotite");
		File[] listOfFiles = folder.listFiles();

		    for (int i = 0; i < listOfFiles.length; i++) {
		      if (listOfFiles[i].isFile()) {
		    	  {
		    		  	System.out.println("File=" + listOfFiles[i].getName());
		    		  	NameFileFavoriteInDir.add(listOfFiles[i].getName());
		    	  }
		      } else if (listOfFiles[i].isDirectory()) {
		        System.out.println("Directory " + listOfFiles[i].getName());
		      }
		    }
		
		
	}
	private void ReadFile()
	{
	
		
		
		try {
			
		int i=0;
		while(i<NameFileFavoriteInDir.size())
		{ 
			 map_1 = new HashMap<String, Object>();
			 FileInputStream fs = new FileInputStream(PathFile+NameFileFavoriteInDir.get(i));  
			  DataInputStream ds = new DataInputStream(fs);
			  BufferedReader br = new BufferedReader(new InputStreamReader(ds));
			  String Text; 
			  int i2=0;
			  while ((Text = br.readLine()) != null)   {

				  
				   if(i2==0)
				   {
					   map_1.put("title",Text);
					   List_title.add(Text);
				   }
				   else
				   if(i2==1)
				   {
					   
					   
					   List_detail.add(Text);
				   }
				   else
				   if(i2==2)
				   {
					   map_1.put("description",Text);
					   List_description.add(Text);
			       }else
						   	
				   if(i2==3)
				   {
					   List_photo.add(Text);
					   
					   
				   }
				   else   
				   if(i2==4)
				   {
					   map_1.put("thumbnail",Text);
					   List_thumbnail.add(Text);
				   }
				   else   
					   if(i2==5)
					   {
						   
						   List_link.add(Text);
					   }
				   
				  
				   
				   
				  Log.e("NT","Text="+i2+"="+Text);
				  
				  
				  
				  i2++;
			  }  
			  ds.close();
			  i++;
			  
			  MyArrList_home.add(map_1);	
		}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId()==R.id.Img_home)
		{
			finish();	
			overridePendingTransition (R.anim.fade_in, R.anim.fade_out);
		}
		
		if(v.getId()==R.id.Img_mul)
		{
			finish();	
			startActivityForResult(new Intent(FavoritesActivity.this,MulActivity.class), 0);
			overridePendingTransition (R.anim.fade_in, R.anim.fade_out);
		}
		
		if(v.getId()==R.id.Img_opinion)
		{
			finish();	
			startActivityForResult(new Intent(FavoritesActivity.this,OpinionActivity.class), 0);
			overridePendingTransition (R.anim.fade_in, R.anim.fade_out);
		}
		if(v.getId()==R.id.Img_setting)
		{
			finish();	
			startActivityForResult(new Intent(FavoritesActivity.this,SettingActivity.class), 0);
			overridePendingTransition (R.anim.fade_in, R.anim.fade_out);
		}
		
		
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
		// TODO Auto-generated method stub
		
		Control.news_description=List_description.get(position);
		Control.news_detail=List_detail.get(position);
		Control.news_link=List_link.get(position);
		Control.news_photo=List_photo.get(position);
		Control.news_title=List_title.get(position);
		
		Log.e("NT", "position="+position);
		
		startActivityForResult(new Intent(FavoritesActivity.this,NewsDetailActivity.class), 0);
	}
	
	
	
	
	class LoadFavorite extends AsyncTask<Object, Integer, Object> {

		@SuppressWarnings("null")
		@Override
		protected Object doInBackground(Object... params) {

			//=============File================
			
			ReadFile();

			return null;
		}

		@Override
		protected void onPreExecute() {

			if (adapter == null) {
				adapter = new OpiListAdapter(FavoritesActivity.this, MyArrList_home);
				lstView.setAdapter(adapter);
			}

			if(mProgressDialog!=null)
				mProgressDialog.show();
			
			
			super.onPreExecute();
		}

		@Override
		public void onProgressUpdate(Integer... progress) {

			Log.e("star", "progressupdate");
			
			
		}

		@Override
		protected void onPostExecute(Object result) {

			
			setProgressBarIndeterminateVisibility(false); // When Finish
			
			 runOnUiThread (new Thread(new Runnable() { 
		         public void run() {
		                adapter.notifyDataSetChanged();    
		               
		         }
		     }));
			 
			 if(mProgressDialog != null)
		            mProgressDialog.dismiss();
		}

	}

	

}
